package user;

import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import constant.Constant;
import utils.AnalyzingBorrowedBook;
import utils.ScannerSingleton;

import java.io.IOException;
import java.util.Scanner;

public class NormalUser extends User{
    //所有用户已借阅的图书相关信息
    private PairOfUidAndBookId[] pairOfUidAndBookIds;
    //当前书籍的借阅量
    private int borroedCount;

    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();

    private Scanner scanner = null;
    private Library library = null;
    public NormalUser(String name, int userID) {
        super(name, userID, "普通用户");
        scanner = ScannerSingleton.getScanner();
        library = Library.getLibrary();
        loadBorrowedBook();
    }

    private void loadBorrowedBook() {
        try {
            PairOfUidAndBookId[] allBorrowedBook = analyzingBorrowedBook.loadObject(Constant.BORROWED_BOOOK_FILE_NAME);
            pairOfUidAndBookIds = new PairOfUidAndBookId[Constant.BORROWED_BOOK_MAX_NUM];
            if (allBorrowedBook == null) {
                borroedCount = 0;
            }else {
                int allBorrowedBooklen = allBorrowedBook.length;
                if (allBorrowedBooklen > pairOfUidAndBookIds.length) {
                    pairOfUidAndBookIds = new PairOfUidAndBookId[allBorrowedBooklen];
                }
                for (int i = 0; i < allBorrowedBooklen; i++) {
                    pairOfUidAndBookIds[i] = allBorrowedBook[i];
                }
                borroedCount = allBorrowedBooklen;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void storeBorrowedBook() {
        try {
            analyzingBorrowedBook.storeObject(pairOfUidAndBookIds, Constant.BORROWED_BOOOK_FILE_NAME);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int display() {
        System.out.println("普通用户 " + getName() + " 的操作菜单：");
        System.out.println("1.查找图书");
        System.out.println("2.打印所有图书");
        System.out.println("3.退出系统");
        System.out.println("4.借阅图书");
        System.out.println("5.归还图书");
        System.out.println("6.查看当前个人借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //借阅图书
    public void borrowBook() {
        System.out.println("Normal类borrowBook方法执行了......");
        scanner.nextLine();
        System.out.println("请输入你要借阅的图书id：");
        int bookid = scanner.nextInt();
        scanner.nextLine();//吞掉换行符
        //如果书架上没有书 不能借阅
        if (library.getBookCount() == 0) {
            System.out.println("书架没有书籍暂且不能借阅!");
            return;
        }
        loadBorrowedBook();
        //判断要借阅的书籍 是否存在
        Book book = library.searchBookByID(bookid);
        if (book == null) {
            System.out.println("没有该id的相关书籍：" + bookid);
        }
        for (int i = 0; i < borroedCount; i++) {
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
            if (pairOfUidAndBookId.getBookId() == book.getBookID()) {
                if (getUserID() == pairOfUidAndBookId.getUserId()) {
                    System.out.println("该书籍已经被你借阅，你的ID是：" + getUserID());
                    return;
                }else {
                    System.out.println("该书籍已被别人借阅，他的ID是：" + pairOfUidAndBookId.getUserId());
                    return;
                }
            }
        }
        library.borrowBook(bookid);
        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(getUserID(),book.getBookID());
        pairOfUidAndBookIds[borroedCount++] = pairOfUidAndBookId;
        storeBorrowedBook();
        System.out.println("借阅成功！");
    }
    //归还图书
    public void returnBook() {
        loadBorrowedBook();
        if (borroedCount == 0) {
            System.out.println("目前没有用户借阅过书籍！");
            return;
        }
        scanner.nextLine();
        System.out.println("请输入你要归还图书的id：");
        int bookId = scanner.nextInt();
        scanner.nextLine();
        Book book = library.searchBookByID(bookId);
        if (book == null) {
            System.out.println("没有该id的相关书籍：" + bookId);
            return;
        }
        for (int i = 0; i < borroedCount; i++) {
            if (pairOfUidAndBookIds[i].getBookId() == bookId) {
                if (pairOfUidAndBookIds[i].getUserId() == getUserID()) {
                    library.returnBook(bookId);
                    System.out.println("图书 " + book.getTitle() + " 已成功归还！");
                    //用最后一本替换归还的书
                    pairOfUidAndBookIds[i] = pairOfUidAndBookIds[borroedCount - 1];
                    //清空最后一个
                    pairOfUidAndBookIds[borroedCount - 1] = null;
                    borroedCount--;
                    storeBorrowedBook();
                }else {
                    System.out.println("该书籍不是你借阅的书籍，不能归还：" + book.getTitle());
                }
                return;
            }
        }
        System.out.println("你没有借阅该书籍，不需要归还，书籍ID为：" + bookId);
    }
    //查看个人借阅情况
    public void viewBorrowBooks() {
        loadBorrowedBook();
        System.out.println("您的借阅情况如下：");
        if (borroedCount == 0) {
            System.out.println("目前没有借阅记录...");
        }else {
            boolean flg = false;
            for (int i = 0; i < borroedCount; i++) {
                if (pairOfUidAndBookIds[i].getUserId() == getUserID()) {
                    flg = true;
                    Book book = library.searchBookByID(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!flg) {
                System.out.println("你没有借阅过书籍！");
            }
        }
    }
}
